//
//  HistoryShowTableViewCell.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/24.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "HistoryShowTableViewCell.h"
#import "HistoryCollectionViewCell.h"
#import "HistoryModel.h"

@interface HistoryShowTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *showCollection;

@end

@implementation HistoryShowTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
        
        flowlayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
        
         flowlayout.sectionInset=UIEdgeInsetsMake(10, 15, 10, 15);
        
        self.showCollection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) collectionViewLayout:flowlayout];
        
        self.showCollection.delegate=self;
        
        self.showCollection.dataSource=self;
        
        self.showCollection.backgroundColor=[UIColor whiteColor];
        
        [self.showCollection registerNib:[UINib nibWithNibName:@"HistoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HCcell"];
        
        [self.contentView addSubview:self.showCollection];
    }
    
    return self;
}


-(void)setDataArr:(NSArray *)dataArr
{
    _dataArr=dataArr;
    
    [self.showCollection reloadData];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArr.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryModel *model=self.dataArr[indexPath.row];
    
    HistoryCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HCcell" forIndexPath:indexPath];
    
    cell.TipLabel.text=model.searchkey;
    
    cell.contentView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    cell.contentView.layer.cornerRadius=3.0;
    
    cell.contentView.layer.masksToBounds=YES;
    
    return cell;
    
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 5;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     HistoryModel *model=self.dataArr[indexPath.row];
    
    _block(model.searchkey);
    
}

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//
//    return 5;
//
//
//}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
     HistoryModel *model=self.dataArr[indexPath.row];


    return CGSizeMake([model.searchkey widthForFont:[UIFont systemFontOfSize:17]]+10, 30);
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
